package logic

import (
	"context"
	"github.com/google/uuid"
	"srv/mooc"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateOrderLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewCreateOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateOrderLogic {
	return &CreateOrderLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *CreateOrderLogic) CreateOrder(req *types.CreateOrderRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line

	info, err := l.svcCtx.MoocSrv.GoodsInfo(l.ctx, &mooc.GoodsInfoRequest{GoodsId: req.GoodsId})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}
	amount := info.GoodsPrice
	orderSn := uuid.NewString()
	userId := 1
	order, err := l.svcCtx.MoocSrv.CreateOrder(l.ctx, &mooc.CreateOrderRequest{
		UserId:      int64(userId),
		GoodsId:     req.GoodsId,
		Num:         req.Num,
		OrderSn:     orderSn,
		OrderAmount: amount,
	})
	if err != nil {
		return &types.Response{
			Code: 500,
			Msg:  err.Error(),
			Data: nil,
		}, nil
	}

	return &types.Response{
		Code: 200,
		Msg:  "下单成功",
		Data: map[string]interface{}{
			"order": order,
		},
	}, nil
}
